package org.zch.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zch.common.ApiResult;
import org.zch.common.utils.MD5Utils;
import org.zch.common.utils.SengSmsUtils;
import org.zch.entity.po.GoodsPO;
import org.zch.entity.po.UserInfoPO;
import org.zch.entity.vo.LaoMoneyVO;
import org.zch.entity.vo.SmsReturnVO;
import org.zch.entity.vo.UserInfoVO;
import org.zch.service.GoodsService;
import org.zch.service.UserService;
import org.zch.service.WorksService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Objects;

/**
 * 用户信息
 *
 * @author 刘新
 */
@RestController
@RequestMapping("/userData")
@Api(tags = "用户信息")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private WorksService worksService;

    @Resource
    private GoodsService goodsService;

    /**
     * 判断当前用户是否为新用户，新用户APP弹隐私协议，用户同意后才创建新用户
     */
    @ApiOperation(value = "是否为新用户", notes = "是否为新用户")
    @PostMapping("/getUserBean")
    public ApiResult getUserBean(@RequestParam("mac") String mac) {
        return ApiResult.ok(userService.getUserBean(mac));
    }

    /**
     * 创建新用户
     *
     * @param mac 加密后的设备码
     * @return UserInfoPO 用户实体
     */
    @ApiOperation(value = "创建新用户", notes = "创建新用户")
    @PostMapping("/setNewUser")
    public ApiResult setNewUser(@RequestParam("mac") String mac) {
        return ApiResult.ok(userService.setNewUser(mac));
    }


    /**
     * 根据code查询用户信息
     *
     * @param code
     * @return UserInfoPO 用户实体
     */
    @ApiOperation(value = "根据code查询用户信息", notes = "根据code查询用户信息")
    @PostMapping("/getUserById")
    public ApiResult getUserById(@RequestParam("code") String code) {
        return ApiResult.ok(userService.getByCode(code));
    }


    /**
     * 根据id查询用户收货地址
     *
     * @param code
     * @return UserInfoPO 用户实体
     */
    @ApiOperation(value = "根据id查询用户收货地址", notes = "根据id查询用户收货地址")
    @PostMapping("/getUserAddressById")
    public ApiResult getUserAddressById(@RequestParam("code") String code) {
        return ApiResult.ok(userService.getUserAddressById(code));
    }

    /**
     * 根据code查询用户唠额
     */
    @ApiOperation(value = "根据code查询用户唠额", notes = "根据code查询用户唠额")
    @PostMapping("/getUserLaoNumByCode")
    public ApiResult getUserLaoNumByCode(@RequestParam("code") String code) {
        return ApiResult.ok(userService.getUserLaoNumByCode(code));
    }

    /**
     * 根据code查询用户唠币
     */
    @ApiOperation(value = "根据code查询用户唠币", notes = "根据code查询用户唠币")
    @PostMapping("/getUserLaoMoney")
    public ApiResult getUserLaoMoney(@RequestParam("code") String code) {
        return ApiResult.ok(userService.getByCode(code).getLaoMoney());
    }

    /**
     * 根据Code判断当前用户唠币够不够
     * code - 用户Code
     * integral - 所需金额
     * id - 商品ID
     * */
    @ApiOperation(value = "根据Code判断当前用户唠币够不够", notes = "根据Code判断当前用户唠币够不够")
    @PostMapping("/getUserHas")
    public ApiResult getUserHas(@RequestParam("code") String code,@RequestParam("integral") Integer integral,@RequestParam("id") Integer id) {
        LaoMoneyVO mM = new LaoMoneyVO();
        mM.setLaoMoney(0);
        mM.setGetNew("1");
        UserInfoVO infoVO = userService.getByCode(code);
        if (infoVO != null){
            //是否存在该商品，目前只有皮肤，先查皮肤
            boolean hasThis = false; //默认不存在
            GoodsPO byId = goodsService.getById(id);
            if (byId != null){
                String skin = infoVO.getSkin();
                if (skin != null && !skin.isEmpty()){
                    String[] mList = skin.split("-");
                    for (String mS : mList){
                        if (byId.getGoodsImg().contains(mS)){
                            hasThis = true;
                            mM.setGetNew("2");
                        }
                    }
                }
            }

            //不存在，查金额够不够
            if (!hasThis){
                Integer laoMoney = infoVO.getLaoMoney();
                if (laoMoney != null){
                    mM.setLaoMoney(laoMoney);
                    if (integral <= laoMoney){
                        mM.setGetNew("0");
                    }
                }
            }

        }
        return ApiResult.ok(mM);
    }

    /**
     * 根据code查询用户皮肤
     */
    @ApiOperation(value = "根据code查询用户皮肤", notes = "根据code查询用户皮肤")
    @PostMapping("/getUserSkin")
    public ApiResult getUserSkin(@RequestParam("code") String code) {
        return ApiResult.ok(userService.getByCode(code).getSkin());
    }


    /**
     * 根据code修改用户唠额
     * Map.get()
     *
     * @return
     */
    @ApiOperation(value = "根据code修改用户唠额", notes = "根据code修改用户唠额")
    @PostMapping("/setUserLaoNumByCode")
    public ApiResult setUserLaoNumByCode(@RequestBody UserInfoPO updatePO) {
        Integer mCode = -1;
        String md5 = MD5Utils.toMD5(updatePO.getCode());
        if(Objects.equals(updatePO.getName(),md5)){
            mCode = userService.setUserLaoNumByCode(updatePO);
        }
        return ApiResult.ok(mCode);
    }

    /**
     * 根据id查询用户手机号
     *
     * @param code
     * @return UserInfoPO 用户实体
     */
    @ApiOperation(value = "根据id查询用户手机号", notes = "根据id查询用户手机号")
    @PostMapping("/getUserPhoneById")
    public ApiResult getUserPhoneById(@RequestParam("code") String code) {
        return ApiResult.ok(userService.getUserPhoneById(code));
    }

    /**
     * 根据mac查询用户信息
     */
    @ApiOperation(value = "根据mac查询用户信息", notes = "根据mac查询用户信息")
    @PostMapping("/getUserByMac")
    public ApiResult getUserByMac(@RequestParam("mac") String mac) {
        return ApiResult.ok(userService.getUserByMac(mac));
    }


    /**
     * 根据用户编码修改用户昵称
     */
    @ApiOperation(value = "根据用户编码修改用户昵称", notes = "根据用户编码修改用户昵称")
    @PostMapping("/upDateUserName")
    public ApiResult upDateUserName(@RequestParam("name") String name, @RequestParam("code") String code) {
        UserInfoPO userInfoPO = new UserInfoPO();
        userInfoPO.setCode(code);
        userInfoPO.setName(name);
        return ApiResult.ok(userService.update(userInfoPO));
    }

    /**
     * 修改用户头像
     */
    @ApiOperation(value = "修改用户头像", notes = "修改用户头像")
    @PostMapping("/upDateUserHeadPic")
    public ApiResult updateUserHeadPic(@RequestParam("headPic") String headPic, @RequestParam("code") String code) {
        return ApiResult.ok(userService.updateUserHeadPic(headPic, code));
    }

    /**
     * 修改用户生日
     */
    @ApiOperation(value = "修改用户生日", notes = "修改用户生日")
    @PostMapping("/upDateUserOld")
    public ApiResult upDateUserOld(@RequestParam("old") String old, @RequestParam("code") String code) {
        UserInfoPO userInfoPO = new UserInfoPO();
        userInfoPO.setCode(code);
        userInfoPO.setOld(old);
        return ApiResult.ok(userService.update(userInfoPO));
    }


    /**
     * 修改用户性别
     */
    @ApiOperation(value = "修改用户性别", notes = "修改用户性别")
    @PostMapping("/upDateUserSex")
    public ApiResult upDateUserSex(@RequestParam("sex") String sex, @RequestParam("code") String code) {
        UserInfoPO userInfoPO = new UserInfoPO();
        userInfoPO.setCode(code);
        userInfoPO.setSex(Integer.valueOf(sex));
        return ApiResult.ok(userService.update(userInfoPO));
    }

    /**
     * 修改用户收货地址
     */
    @ApiOperation(value = "修改用户收货地址", notes = "修改用户收货地址")
    @PostMapping("/upDateUserAddress")
    public ApiResult upDateUserAddress(@RequestParam("address") String address, @RequestParam("code") String code) {
        UserInfoPO userInfoPO = new UserInfoPO();
        userInfoPO.setCode(code);
        userInfoPO.setAddress(address);
        return ApiResult.ok(userService.update(userInfoPO));
    }

    /**
     * 发送手机验证码
     */
    @ApiOperation(value = "发送手机验证码", notes = "发送手机验证码")
    @PostMapping("/sendSMS")
    public ApiResult sendSMS(@RequestParam("phone") String phone) {
        //六位数随机数
        long rand = (long)(Math.random()*900000+100000);
        String mCode = String.valueOf(rand);
        boolean mS = SengSmsUtils.sendSMS(phone,mCode);
        //返回数据
        SmsReturnVO smsReturnVO = new SmsReturnVO();
        smsReturnVO.setSendSuccess(true);
        smsReturnVO.setMCode(MD5Utils.toMD5(mCode));
        if (!mS){
            smsReturnVO.setSendSuccess(false);
        }
        return ApiResult.ok(smsReturnVO);
    }


    /**
     * 修改用户手机号
     */
    @ApiOperation(value = "修改用户手机号", notes = "修改用户手机号")
    @PostMapping("/upDateUserPhone")
    public ApiResult upDateUserPhone(@RequestParam("phone") String phone) {
        String code = "-1";
        try {
            //先判断该手机号是否存在
            UserInfoPO userPO = userService.getUserByMac(phone);
            if (userPO != null) {
                //已存在
                code = userPO.getCode();
            }else {
                //首先创建一个用户
                String userCode = userService.setNewUser(phone);
                //其次在修改该用户的手机号
                UserInfoPO userInfoPO = new UserInfoPO();
                userInfoPO.setCode(userCode);
                userInfoPO.setPhone(phone);
                userService.update(userInfoPO);
                code = userCode;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return ApiResult.ok(code);
    }

    /**
     * 修改用户是否注销
     */
    @ApiOperation(value = "修改用户是否注销", notes = "修改用户是否注销")
    @PostMapping("/upDateUserOut")
    public ApiResult upDateUserOut(@RequestParam("hasOut") String hasOut, @RequestParam("code") String code) {
        Boolean hasD = true;
        if (!"true".equals(hasOut)) {
            hasD = false;
        }
        UserInfoPO userInfoPO = new UserInfoPO();
        userInfoPO.setCode(code);
        userInfoPO.setHasOut(hasD);
        return ApiResult.ok(userService.update(userInfoPO));
    }

    /**
     * 根据用户mac获取用户id
     *
     * @param mac 设备码
     * @return UserInfoPO 用户实体
     */
    @ApiOperation(value = "根据用户mac获取用户id", notes = "根据用户mac获取用户id")
    @GetMapping("/getUserIdByMac")
    public ApiResult getUserIdByMac(@RequestParam("mac") String mac) {
        return ApiResult.ok(userService.getUserIdByMac(mac));
    }

    /**
     * 查询所有
     *
     * @return UserInfoPO 用户实体
     */
    @ApiOperation(value = "查询所有", notes = "查询所有")
    @GetMapping("/findAll")
    public ApiResult findAll(UserInfoPO userInfoPO) {
        return ApiResult.ok(userService.search(userInfoPO));
    }


    /**
     * 排行榜
     */
    @ApiOperation(value = "排行榜", notes = "排行榜")
    @PostMapping("/getUserRanking")
    public ApiResult getUserRanking(@RequestParam("code") String code) {
        return ApiResult.ok(userService.getUserRanking(code));
    }


    /**
     * 获取签到数据
     */
    @ApiOperation(value = "获取签到数据", notes = "获取签到数据")
    @PostMapping("/getUserSign")
    public ApiResult getUserSign(@RequestParam("code") String code) {
        return ApiResult.ok(userService.getUserSign(code));
    }

    /**
     * 签到
     */
    @ApiOperation(value = "签到", notes = "签到")
    @PostMapping("/setUserSign")
    public ApiResult setUserSign(@RequestParam("code") String code, @RequestParam("signNum") String signNum) {
        return ApiResult.ok(userService.setUserSign(code, Integer.parseInt(signNum)));
    }


    /**
     * 添加用户的唠币
     * */
    @ApiOperation(value = "添加唠额", notes = "添加唠额")
    @PostMapping("/addUserLE")
    public ApiResult addUserLE(@RequestParam("code") String code,@RequestParam("check") String check) {
        Integer mCode = -1;
        String md5 = MD5Utils.toMD5(code);
        if(Objects.equals(check,md5)){
            mCode = userService.updateLaoMoney(code, 1);
        }
        return ApiResult.ok(mCode);
    }

}
